package com.bytz.modules.cms.domain.impl.price;

import com.bytz.modules.cms.domain.model.price.PriceComputeContext;
import com.bytz.modules.cms.domain.model.price.PriceComputeResult;
import com.bytz.modules.cms.productLine.service.IProductLineLevelLimitService;
import com.bytz.modules.cms.reseller.service.IResellerLevelService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 产品线限制产品计算器，顺序 2
 *
 * @author shanxuezhong
 */
@Component
public class ProductLineLimitComputer extends AbstractPriceComputer {
    @Autowired
    private IResellerLevelService resellerLevelService;

    @Autowired
    private IProductLineLevelLimitService productLineLevelLimitService;

    @Override
    public void doCompute(PriceComputeContext context) {
        ensureResellerLevels(resellerLevelService, context);
        Map<String, String> resellerLevelMap = context.getResellerLevelMap();
        if (MapUtils.isEmpty(resellerLevelMap)) {
            return;
        }
        Collection<String> productLineLevel = resellerLevelMap.values();
        Set<String> hasPricePns = context.getHasPricePns();
        List<String> limitPns = productLineLevelLimitService.getByProductNumbersAndProductLineIdAndLevelId(hasPricePns, productLineLevel);
        Map<String, PriceComputeResult> results = context.getResults();
        for (String limitPn : limitPns) {
            PriceComputeResult priceComputeResult = results.get(limitPn);
            if (priceComputeResult != null) {
                priceComputeResult.setPrice(null);
            }
        }

    }

    @Override
    public void afterCompute(PriceComputeContext context) {
        super.afterCompute(context);
    }
}